package com.winning.devops.batch.reader.ch02.config;

import com.winning.devops.batch.reader.ch02.domain.User;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.RowMapper;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 使用
 *
 * @author chensj
 * @date 2020-02-29 20:30
 */
@Configuration
public class DatabaseJdbcItemReaderConfig {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private DataSource dataSource;
    @Autowired
    @Qualifier("databaseJdbcDemoItemWriter")
    private ItemWriter<? super User> databaseJdbcDemoItemWriter;

    @Bean
    public Job databaseJdbcDemoJob() {
        return jobBuilderFactory.get("databaseJdbcDemoJob")
                .start(databaseJdbcDemoStep())
                .build();
    }

    @Bean
    public Step databaseJdbcDemoStep() {
        return stepBuilderFactory
                .get("databaseJdbcDemoStep")
                .<User, User>chunk(2)
                .reader(databaseJdbcDemoItemReader())
                .writer(databaseJdbcDemoItemWriter)
                .build();
    }

    @Bean
    @StepScope
    public JdbcPagingItemReader<User> databaseJdbcDemoItemReader() {
        JdbcPagingItemReader<User> reader = new JdbcPagingItemReader<>();
        reader.setDataSource(dataSource);
        reader.setPageSize(2);
        // 数据库记录转换实体类
        reader.setRowMapper(new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {

                User user = new User();
                user.setId(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setAge(rs.getInt(4));
                return user;
            }
        });
        // 指定查询语句 SQL
        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        // 指定查询那些字段
        provider.setSelectClause("ID,USER_NAME,PASSWORD,AGE");
        // 指定查询那张表
        provider.setFromClause("FROM SYS_USER");
        // 指定根据那个字段进行排序
        Map<String, Order> sortKeys = new HashMap<>(1);
        // 设置ID排序
        sortKeys.put("ID", Order.ASCENDING);
        provider.setSortKeys(sortKeys);
        // 指定数据库查询信息
        reader.setQueryProvider(provider);
        return reader;
    }
}
